<?php
/**
 * PHP 时间处理函数
 * 修改编码格式:统一编码：UTF-8
 * @author Tujt 2015-09-18
 */
class PHPDate {
	public static $date = '';
	public static $time = '';
	public static $datetime = '';
	private static $_instance;//单例返回类实例
	private static $send_start_time = '';//开始执行时间
    public function  __construct(){

    }
	public static function getInstance() {
		/**
		 * 单例返回当前类的实例
		 * @author Tujt 2014-07-24
		 */
		if(!(self::$_instance instanceof self)){//是否为当前类的实例
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	/**
	 * @author Tujt 2015-09-18
	 */
	public static function init() {
		if(empty(self::$time)) self::$time = time();
	}
	/**
	 * @author Tujt 2015-09-18
	 */
	public static function set($set='') {
		if(empty($set)){
			self::$time = time();
		}else{
			if(is_numeric($set)){//time 时间戳
				self::$time = $set;
			}else{
				self::$time = strtotime($set);
			}
		}
		return self::getInstance();
	}
	/**
	 * 获取日期
	 * @author Tujt 2015-09-18
	 */
	public static function get($get='Y-m-d') {
		self::init();
		$time = self::$time;
		self::unsetInstance();
	    return date($get,$time);
	}
	/**
	 * 获取时间
	 * @author Toby.tu 2016-08-11
	 */
	public static function time(){
		return self::get('Y-m-d H:i:s');
	}
	/**
	 * 初始化年
	 * @author Tujt 2015-09-18
	 */
	public static function year($month) {
	    self::init();
		self::$time = mktime(date('H',self::$time),date('i',self::$time),date('s',self::$time),date('m',self::$time) ,date('d',self::$time),date('Y',self::$time)+$month);
		return self::getInstance();
	}
	/**
	 * 初始化月份
	 * @author Tujt 2015-09-18
	 */
	public static function month($month) {
	    self::init();
		self::$time = mktime(date('H',self::$time),date('i',self::$time),date('s',self::$time),date('m',self::$time)+$month ,date('d',self::$time),date('Y',self::$time));
		return self::getInstance();
	}
	/**
	 * 初始化日期
	 * @author Tujt 2015-09-18
	 */
	public static function day($month) {
	    self::init();
		self::$time = mktime(date('H',self::$time),date('i',self::$time),date('s',self::$time),date('m',self::$time) ,date('d',self::$time)+$month,date('Y',self::$time));
		return self::getInstance();
	}
	/**
	 * 调整小时
	 * @author Toby.Tu 2015-09-18
	 */
	public static function hour($hour) {
	    self::init();
		self::$time = mktime(date('H',self::$time)+$hour,date('i',self::$time),date('s',self::$time),date('m',self::$time) ,date('d',self::$time),date('Y',self::$time));
		return self::getInstance();
	}
	/**
	 * 调整分钟
	 * @author Toby.Tu 2015-09-18
	 */
	public static function minute($minute) {
	    self::init();
		self::$time = mktime(date('H',self::$time),date('i',self::$time)+$minute,date('s',self::$time),date('m',self::$time) ,date('d',self::$time),date('Y',self::$time));
		return self::getInstance();
	}
	/**
	 * 调整秒钟
	 * @author Toby.Tu 2015-09-18
	 */
	public static function second($second) {
	    self::init();
		self::$time = mktime(date('H',self::$time),date('i',self::$time),date('s',self::$time)+$second,date('m',self::$time) ,date('d',self::$time),date('Y',self::$time));
		return self::getInstance();
	}
	/**
	 * 获取时间戳
	 * @author Tujt 2015-11-23
	 */
	public static function getTime() {
		self::init();
		$time = self::$time;
		self::unsetInstance();
	    return $time;
	}
	/**
	 * 获取毫秒时间戳
	 * @author Tujt 2015-11-23
	 */
	public static function mtime() {
		list($usec, $sec) = explode(" ", microtime());
		$mtime = ((float)$usec + (float)$sec);
		$mtimearr = explode('.', $mtime);
		return $mtimearr[0].'.'.sprintf('%04d',$mtimearr[1]);
	}
	/**
	 * 获取两个时间段内的所有时间
	 * @author Tujt 2015-11-25
	 */
	public static function getDays($startday,$endday) {
	    if(empty($startday) || empty($endday)){
			return [];
		}
		if($startday == $endday){
			return [$startday];
		}
		$days = array();
		$days[] = $startday;
		do{
			$days[] = $startday = self::set($startday)->day(1)->get('Y-m-d');
		}while($startday != $endday);
		self::unsetInstance();
		return $days;
	}
	/**
	 * 获取两个时间段内的所有时间
	 * @author Tujt 2015-11-25
	 */
	public static function getMonths($startday,$endday) {
	    if(empty($startday) || empty($endday)){
			return [];
		}
		$startMonth = date('Y-m',strtotime($startday));
		$endMonth = date('Y-m',strtotime($endday));
		if($startMonth == $endMonth){
			return [$startMonth];
		}
		$months = array();
		$months[] = $startMonth;
		do{
			$months[] = $startMonth = self::set($startMonth)->month(1)->get('Y-m');
		}while($startMonth != $endMonth);
		self::unsetInstance();
		return $months;
	}
	/**
	 * 获取本周开始结束时间
	 * @author Toby.Tu 2016-04-21
	 */
	public static function getWeeks($idx=0) {
		self::init();
		$idx = $idx*7;
		$w = date('w',self::$time);
		if(!$w) $w = 7;//周日是0
		$day = date('d',self::$time)-$w+1+$idx;
		$time = mktime(0, 0 , 0,date('m',self::$time),$day,date('Y',self::$time));
		$first = self::set($time)->get('Y-m-d');
		$last = self::set($first)->day(6)->get('Y-m-d');
		return array($first,$last);
	}
	/**
	 * 销毁Instance对象
	 * @author Tujt 2015-11-25
	 */
	public static function unsetInstance() {
		self::$time = 0;
	}
	/**
	 * 检查时间格式
	 * @author Tujt 2015-11-25
	 */
	public static function check($date) {
	    if(empty($date) || empty(strtotime($date)) || strtotime($date) < 0){
			return false;
		}
		return true;
	}
	/**
	 * 相差日期
	 * @author Tujt 2015-11-25
	 */
	public static function diff($start,$end,$type='day') {
	    $start_time = self::set($start)->getTime();
		$end_time = self::set($end)->getTime();
		$diff = 0;
		switch($type){
		    case 'd':
		    case 'day'://相差天数
		        $diff = round(($end_time - $start_time)/3600/24);
		        break;
			case 'm':
		    case 'month'://相差月份
		        $diff = round(($end_time - $start_time)/3600/24/30);
		        break;
			case 'y':
			case 'year'://相差年度
		        $diff = round(($end_time - $start_time)/3600/24/30/12);
		        break;
			case 'h':
			case 'hour':
			    $diff = round(($end_time - $start_time)/3600);
			    break;
			case 'i':
			case 'minute':
			    $diff = round(($end_time - $start_time)/60);
			    break;
			case 's':
			case 'seconds':
				$diff = round($end_time - $start_time);
			    break;
		}
		return $diff;
	}
	/**
	 * 记录当前时间
	 * @author Tujt 2015-03-06
	 */
	public static function start() {
	    self::$send_start_time = microtime();
	}
	/**
	 * 计算某段代码的执行时间，需要跟start方法一起调用
	 * @author Tujt 2015-03-06
	 */
	public static function end() {
		$stime = self::$send_start_time;
		self::$send_start_time = $etime = microtime();
	    $starttime = explode(' ',$stime);
		$endtime = explode(' ',$etime);
		$totaltime = $endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];
		$timecost = sprintf('%.4f',$totaltime);
		return $timecost;//执行的秒数
	}
	public function  __destruct(){

    }
}
?>
